package com.zf.shuashua.ui.mine.notice;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SimpleItemAnimator;

import com.meis.base.mei.adapter.MeiBaseAdapter;
import com.meis.base.mei.base.BaseListFragment;
import com.meis.base.mei.entity.Result;
import com.meis.base.mei.utils.ParseJsonUtils;
import com.zf.shuashua.R;
import com.zf.shuashua.api.AbsCallback;
import com.zf.shuashua.api.HttpData;
import com.zf.shuashua.api.Store;
import com.zf.shuashua.bean.BaseData;
import com.zf.shuashua.bean.FansList;
import com.zf.shuashua.bean.FocusType;
import com.zf.shuashua.ui.mine.FriendsDetailsActivity;
import com.zf.shuashua.ui.mine.LogUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import io.reactivex.Observable;

/**
 * 消息-粉丝列表
 */
public class FeedListFragment extends BaseListFragment<FansList.DataBean> {

    private RecyclerView mRecyclerView;
    private MessageFansListAdapter mAdapter;

    public static FeedListFragment getFragment(){
        return new FeedListFragment();
    }

    @Override
    protected void initView() {
//        setState(ViewState.EMPTY);
        setEmptyLayout(R.layout.empty_feed_list);
        super.initView();
    }

    @Override
    protected void initData() {
        loadPage(1);
    }

    @Override
    protected Observable<Result<List<FansList.DataBean>>> getListObservable(int pageNo) {
        Observable<String> observable = HttpData.getInstance().getNoticeFansList(pageNo);
        return getListData(observable);
    }

    private Observable<Result<List<FansList.DataBean>>> getListData(Observable<String> observable) {
        final Result<List<FansList.DataBean>> result = new Result<>();
        return observable.map(s -> {
            if (!TextUtils.isEmpty(s)) {
                try {
                    FansList fansList = ParseJsonUtils.parseData(s, FansList.class);
                    result.data = fansList.getData();
                } catch (Exception e) {
                }
            }
            return result;
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_list_search_video;
    }

    @Override
    protected RecyclerView getRecyclerView() {
        mRecyclerView = findViewById(R.id.recycler);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        ((SimpleItemAnimator) Objects.requireNonNull(mRecyclerView.getItemAnimator())).setSupportsChangeAnimations(false);
        return mRecyclerView;
    }

    @Override
    protected MeiBaseAdapter<FansList.DataBean> getAdapter() {
        mAdapter = new MessageFansListAdapter(R.layout.message_fans_item, null);
        mAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            FansList.DataBean dataBean = (FansList.DataBean) adapter.getData().get(position);
            if (dataBean.getFans() != null) {
                HttpData.getInstance().getFocusType(dataBean.getFans().getId(), new AbsCallback<BaseData<FocusType>>() {
                    @Override
                    public void onRealSuccess(BaseData<FocusType> data) {
                        switch (data.getData().getFollow_status()) {
                            case 0://未关注
                                view.setBackgroundResource(R.drawable.fans_off);
                                ((TextView) view).setText("关注");
                                break;
                            case 1://已关注
                            case 2://相互关注
                                view.setBackgroundResource(R.drawable.fans_no);
                                ((TextView) view).setText(data.getData().getFollow_status_text());
                                break;
                        }
                        dataBean.setFollow_status(data.getData().getFollow_status());
                        mAdapter.notifyItemChanged(position);
//                            if (listener != null) {
//                                listener.listener(refresh);
//                            }
//                            getActivity().setResult(RESULT_OK);
                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {
                        LogUtil.d(errMsg);

                    }
                });

            }
        });
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            FansList.DataBean dataBean = (FansList.DataBean) adapter.getData().get(position);
            if (dataBean.getFans() != null) {
                mActivity.startActivity(new Intent(getActivity(), FriendsDetailsActivity.class).putExtra("USER_ID", dataBean.getFans().getId() + ""));
            }
        });
        return mAdapter;
    }

    @Override
    public boolean canLoadMore() {
        return true;
    }

    @Override
    public boolean canPullToRefresh() {
        return false;
    }
}
